#include "json/json-usr.h"

namespace iecas_ns
{
    shared_ptr<CJsonUsr> CJsonUsr::sm_intancePtr = nullptr;

    CJsonUsr::CJsonUsr()
    {
        m_commonPtr = shared_ptr<CCommon>(CCommon::getInstance());
        allParams.clear();
        tcp_server_port = 0;
        huawei_aos_ip.clear();
        http_camera_port = 0;
    }

	shared_ptr<CJsonUsr> CJsonUsr::getInstance()
	{
		if(sm_intancePtr == nullptr)
		{
			sm_intancePtr = shared_ptr<CJsonUsr>(new CJsonUsr());
		}
		return sm_intancePtr;
	}

    bool CJsonUsr::init(void)
    {
        readFromJsonFile(PARAMS_JSON_FILE_NAME, allParams);
    }

    bool CJsonUsr::readFromJsonFile(string fileName, Value &fileContent)
    {
        JSONCPP_STRING errs;
        CharReaderBuilder readBuilder;
        fstream newJsonFile(fileName);

        if(!newJsonFile)
        {
            cout << "open json file failed: " << fileName << endl;
            PLOGD << "open json file failed: " << fileName;
            return false;
        }

        bool res = parseFromStream(readBuilder, newJsonFile, &fileContent, &errs);
        if(!res || !errs.empty())
        {
            cout << "read json file failed: " << fileName << endl;
            PLOGD << "read json file failed: " << fileName;
            return false;
        }
        else
        {
            newJsonFile.close();
            return true;
        }
    }

    bool CJsonUsr::writeToJsonFile(string fileName, Value fileContent)
    {
        StreamWriterBuilder writeBuilder;
        const unique_ptr<StreamWriter> newWrite(writeBuilder.newStreamWriter());

        fstream newJsonFile(fileName, ios::out | ios::trunc);
        if(!newJsonFile)
        {
            cout << "open json file failed: " << fileName << endl;
            PLOGD << "open json file failed: " << fileName;
            return false;
        }

        if(newWrite->write(fileContent, &newJsonFile) != 0)
        {
            cout << "write json file failed: " << fileName << endl;
            PLOGD << "write json file failed: " << fileName;
        }
        else
        {
            newJsonFile.close();
            return true;
        }
    }

    void CJsonUsr::start(void)
    {
		if(allParams.isMember(TCP_SERVER_PORT))
		{
			tcp_server_port = allParams[TCP_SERVER_PORT].asInt();
			cout << "get .json " << TCP_SERVER_PORT << ": " << tcp_server_port << endl;
			PLOGD << "get .json " << TCP_SERVER_PORT << ": " << tcp_server_port;
		}
		else
		{
			cout << "get .json " << TCP_SERVER_PORT << " invalid" << endl;
			PLOGD << "get .json " << TCP_SERVER_PORT << " invalid";
		}

        if(allParams.isMember(HUAWEI_AOS_IP))
        {
            int ipArray[4];
            ipArray[0] = allParams[HUAWEI_AOS_IP][0].asInt();
            ipArray[1] = allParams[HUAWEI_AOS_IP][1].asInt();
            ipArray[2] = allParams[HUAWEI_AOS_IP][2].asInt();
            ipArray[3] = allParams[HUAWEI_AOS_IP][3].asInt();
            huawei_aos_ip = to_string(ipArray[0]) + "." + to_string(ipArray[1]) + "."+ to_string(ipArray[2]) + "." + to_string(ipArray[3]);
            cout << "get .json " << HUAWEI_AOS_IP << ": " << huawei_aos_ip << endl;
            PLOGD << "get .json " << HUAWEI_AOS_IP << ": " << huawei_aos_ip;
        }
        else
        {
            cout << "get .json " << HUAWEI_AOS_IP << " invalid" << endl;
            PLOGD << "get .json " << HUAWEI_AOS_IP << " invalid";
        }

        if(allParams.isMember(HTTP_CAMERA_PORT_BASE))
        {
            http_camera_port = allParams[HTTP_CAMERA_PORT_BASE].asInt();
            cout << "get .json " << HTTP_CAMERA_PORT_BASE << ": " << http_camera_port << endl;
            PLOGD << "get .json " << HTTP_CAMERA_PORT_BASE << ": " << http_camera_port;
        }
        else
        {
            cout << "get .json " << HTTP_CAMERA_PORT_BASE << " invalid" << endl;
            PLOGD << "get .json " << HTTP_CAMERA_PORT_BASE << " invalid";
        }
    }

    void CJsonUsr::getAllParams(Value &params)
    {
        params = allParams;
    }
    
    void CJsonUsr::getTcpServerPort(int &port)
    {
        port = tcp_server_port;
    }
    
    void CJsonUsr::getHuaweiAosIp(string &ip)
    {
        ip = huawei_aos_ip;
    }

    void CJsonUsr::getHttpCameraPort(int &port)
    {
        lock_guard<mutex> lockG(CJsonUsr::m_http_camera_port_lock);

        port = http_camera_port;
        ++http_camera_port;
    }
}
